﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HgCo.WindowsLive.SkyDrive.TotalCommander.Plugins.Forms
{
    /// <summary>
    /// Represents a dialog window for connection to SkyDrive.
    /// </summary>
    internal partial class SkyDriveWfxLogOnForm : Form
    {
        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        private static string UserName { get; set; }

        /// <summary>
        /// Gets or sets the password of the user.
        /// </summary>
        /// <value>The password of the user.</value>
        private static string UserPassword { get; set; }

        /// <summary>
        /// Gets or sets the session.
        /// </summary>
        /// <value>The session.</value>
        public WebSession Session { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="SkyDriveWfxLogOnForm"/> class.
        /// </summary>
        internal SkyDriveWfxLogOnForm()
            : base()
        {
            InitializeComponent();

            this.Icon = Resources.SkyDriveWfx.mini_storage;

            txtUserName.Text = UserName;
            txtUserPassword.Text = UserPassword;
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// Handles the Click event of the btnConnect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                Enabled = false;
                UseWaitCursor = true;

                SkyDriveWebClient wcSkyDrive = new SkyDriveWebClient();
                wcSkyDrive.LogOn(txtUserName.Text, txtUserPassword.Text);
                Session = wcSkyDrive.Session;

                UserName = txtUserName.Text;
                UserPassword = txtUserPassword.Text;

                DialogResult = DialogResult.OK;
            }
            catch
            {
                MessageBox.Show("Failed to connect/log on to SkyDrive!");
                DialogResult = DialogResult.None;
            }
            finally
            {
                Enabled = true;
                UseWaitCursor = false;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnDonate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnDonate_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6021108");
        }
    }
}
